package cn.dankal.lequ.ui.fragment.download_fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.dankal.lequ.R;
import cn.dankal.lequ.commom.adapter.BaseAdapter.ViewHolder;
import cn.dankal.lequ.commom.adapter.BaseAdapter.recyclerview.CommonAdapter;
import cn.dankal.lequ.commom.appcomponent.MyApplication;
import cn.dankal.lequ.domain.download.CommonUtils;
import cn.dankal.lequ.domain.download.DownLoadInfo;
import cn.dankal.lequ.domain.download.FileUtils;

/**
 * Created by Dankal on 16/4/21.
 */
public class AlreadyDownloadFragment extends Fragment {
    @Bind(R.id.recyclerview)
    RecyclerView rv_compre;
    LinearLayoutManager linearLayoutManager;
    private List<DownLoadInfo> mDatas;
    private CommonAdapter<DownLoadInfo> commonAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recyclerview, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRecyclerView();

        //检测安装包
        checkDownloadedApk();
    }

    private void checkDownloadedApk() {
        String downloadPath = FileUtils.getDownloadPath();
        File file = new File(downloadPath);
        if (file.exists()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                String name = files[i].getName();
                Log.i("down123", name + "");
                try {
                    Log.i("down123", name.substring(name.lastIndexOf(".")) + "");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if (name.contains(".") && "apk".equals(name.substring(name.lastIndexOf(".")+1))) {

                    DownLoadInfo downLoadInfo = new DownLoadInfo();

                    downLoadInfo.packageName = name;
                    downLoadInfo.savePath = files[i].getPath();
                    Log.i("down123", downLoadInfo.savePath + "");
                    downLoadInfo.maxLength = files[i].length();
                    downLoadInfo.maxProgress = new BigDecimal(files[i].length()).divide(new BigDecimal(1024 * 1024), 2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    mDatas.add(downLoadInfo);
                }

            }
            commonAdapter.notifyDataSetChanged();

        }
    }

    private void initRecyclerView() {
        linearLayoutManager = new LinearLayoutManager(getActivity());
        rv_compre.setLayoutManager(linearLayoutManager);
        rv_compre.setHasFixedSize(true);
        mDatas = new ArrayList<>();

        commonAdapter = new CommonAdapter<DownLoadInfo>(getActivity(), R.layout.item_currency_download, mDatas) {
            @Override
            public void convert(ViewHolder holder, final DownLoadInfo downLoadInfo) {
                holder.setImageResource(R.id.iv_download_game_icon, R.mipmap.wushen);
                holder.setText(R.id.tv_download_game_name, "武神赵子龙——林允儿代言");
                holder.setText(R.id.tv_download_game_version, "版本 1.12 ➡ 1.55");
                holder.setText(R.id.tv_download_game_traffic, downLoadInfo.maxProgress + " MB");
                holder.setText(R.id.tv_download_game_apk, "安装");
                holder.setOnClickListener(R.id.tv_download_game_apk, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        FragmentActivity activity = getActivity();
                        if (activity != null)
                            CommonUtils.installApp(activity,
                                    new File(downLoadInfo.savePath));
                        else
                            Toast.makeText(MyApplication.getAppContext(), "安装失败，请从文件管理器安装", Toast.LENGTH_SHORT).show();

                    }
                });

            }
        };
        rv_compre.setAdapter(commonAdapter);
    }


}
